app.controller('groupViewController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams',
                        '$location', '$anchorScroll','AlertFactory','GridFactory','validationService',
            function ($scope, httpWrapService, dialogs, $state, $stateParams, $location, $anchorScroll, AlertFactory, GridFactory,validationService) {
    $scope.entity = {};

    $scope.id = $stateParams.id;
    $scope.type = $stateParams.type || 'add';
    $scope.Alert = AlertFactory.newInstance();
    $scope.entity.parentId = $stateParams.parentId ? $stateParams.parentId : null;

    //$scope.region = {};

    $scope.openRegionTree = function () {
        dialogs.create('system-tpl\/region\/tree.html','changeRegionTreeCtrl',$scope.entity,{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class',copy : false});
    }
    $scope.openGroupTree = function () {
        dialogs.create('system-tpl\/group\/tree.html','changeGroupTreeCtrl',$scope.entity,{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class',copy : false});
    }

    var columnDefs = [
        {name: "角色名", field: "name"},
        {name: "角色", field: "role"}
    ];

    //$scope.$watch('region.selected', function(newValue, oldValue) {
    //    if(newValue) {
    //        $scope.entity.areaId = $scope.region.selected.id;
    //    }
    //});

     var loadSuccessCallbak = function(){
        // 角色加载成功后，加载数据
        if($scope.id) {
            httpWrapService.post('system/group/view/' + $scope.id + '.do').then(function (res) {
                $scope.entity = res.data.entity;
                $scope.type = 'update';

                // 初始化区域
                //for(var i = 0 ; i < $scope.regions.length; i++) {
                //    var e = $scope.regions[i];
                //    if(e.id == $scope.entity.areaId) {
                //        $scope.region.selected = e;
                //        break;
                //    }
                //}
                // 初始化选择权限
                if(res.data.map.roles) {
                    var roles  = {};
                    for(var i = 0; i < res.data.map.roles.length; i++) {
                        var ra = res.data.map.roles[i];
                        roles[ra.roleId] = ra;
                    }

                    // 选中行
                    $scope.Grid.selectRows(roles);
                }
            });
        }
    }
    $scope.Grid = GridFactory.newInstance({
        url : "system/role/list.do",
        loadSuccessCallbak : loadSuccessCallbak,
        columnDefs : columnDefs,
        gridOptions : {
            multiSelect : true
        }
    });
    $scope.Grid.load();

    $scope.unique = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.Group",
            field : "group",
            value : value,
            id : $scope.id
        })
    }
    $scope.uniqueGroup = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.Group",
            field : "name",
            value : value,
            id : $scope.id
        })
    }

    $scope.save = function () {
        var rows = $scope.Grid.getSelectedRows();
        var roles = [];
        if(rows) {
            for(var i = 0; i < rows.length; i++) {
                var obj = {
                    roleId : rows[i].id,
                    groupId : $scope.id
                }
                roles.push(obj);
            }
        }
        httpWrapService.defaultSave( 'system/group/' + $scope.type + '.do',{
            entity : $scope.entity,
            roles : roles
        }, $scope).then(function (res) {
            $state.go('app.system.group.list');
        });

        // 保存成功返回列表页面

    }

    $scope.canle = function () {
        // 后退到列表页面
        $state.go('app.system.group.list');
    }

}]);

app.controller('changeRegionTreeCtrl',['$scope', '$uibModalInstance','data','httpWrapService','dialogs', function($scope,$uibModalInstance,data, httpWrapService,dialogs){
    $scope.data = data;
    $scope.treeData = [];


    $scope.itemClicked = function(item){
        $scope.data.areaId = item.$item.id;
        $uibModalInstance.close();
    }

    $scope.cancel = function(){
        $uibModalInstance.close('Canceled');
    }; // end cancel

    //$scope.ok = function(){
    //    if($scope.region && $scope.region.id) {
    //        $scope.data.areaId = $scope.region.id;
    //    }
    //    $uibModalInstance.close();
    //};

    httpWrapService.post("system/region/tree.do").then(function(res){
        $scope.treeData = res.data.data;
    });

}]);

app.controller('changeGroupTreeCtrl',['$scope', '$uibModalInstance','data','httpWrapService','dialogs', function($scope,$uibModalInstance,data, httpWrapService,dialogs){
    $scope.data = data;
    $scope.treeData = [];


    $scope.itemClicked = function(item){
        if($scope.data.id != item.$item.id) {
            if($.inArray($scope.data.id, item.$item.parentIds) > 0) {
            } else {
                $scope.data.parentId = item.$item.id;
            }
        }
        $uibModalInstance.close();
    }

    $scope.cancel = function(){
        $uibModalInstance.close('Canceled');
    }; // end cancel

    //$scope.ok = function(){
    //    if($scope.region && $scope.region.id) {
    //        $scope.data.areaId = $scope.region.id;
    //    }
    //    $uibModalInstance.close();
    //};

    httpWrapService.post("system/group/tree.do").then(function(res){
        $scope.treeData = res.data.data;
    });

}]);